package cn.tedu.oa.notice.controller;

import cn.tedu.oa.common.annotation.LogAnnotation;
import cn.tedu.oa.common.vo.JsonResult;
import cn.tedu.oa.notice.entity.Notice;
import cn.tedu.oa.notice.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by UID yangzhou 2018/9/19.
 */
@Controller
@RequestMapping("/notice/")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    //公告首页
    @RequestMapping("notice_ui")
    public String doNoticeUI(){
        return "notice/notice_ui";
    }
    //公告添加页面
    @RequestMapping("notice_edit")
    public String doNoticeEditUI(){
        return "notice/notice_edit";
    }

    //根据id删除数据
    @LogAnnotation("公告：删除操作")
    @RequestMapping("deleteById")
    @ResponseBody
    public JsonResult deleteById(Integer id){
        noticeService.delete(id);
        return new JsonResult("删除成功！");    }

    //根据id(多选)删除
    @LogAnnotation("公告：多项删除操作")
    @RequestMapping("deleteByIds")
    @ResponseBody
    public JsonResult deleteByIds(Integer... ids){
        noticeService.deleteByIds(ids);
        return new JsonResult("删除成功");
    }
    /*获得所有部门id和名称*/
    @LogAnnotation("公告：获取部门id与名称")
    @RequestMapping("getDeptIdAndName")
    @ResponseBody
    public JsonResult getDeptIdAndName() {
        return noticeService.getDeptIdAndName();
    }
    //根据ID查询数据
    @LogAnnotation("公告：根据ID查询操作")
    @RequestMapping("doNoticeById")
    @ResponseBody
    public Notice doNoticeById(Integer id){
        return noticeService.getNoticeById(id);
    }
    //添加公告数据
    @LogAnnotation("公告：添加操作")
    @RequestMapping("add")
    @ResponseBody
    public JsonResult add(Notice notice){
        noticeService.add(notice);
        return new JsonResult("添加成功！");
    }
    //修改数据
    @LogAnnotation("公告：修改操作")
    @RequestMapping("updateNotice")
    @ResponseBody
    public JsonResult updateNotice(Notice notice){
        noticeService.updateNotice(notice);
        return new JsonResult("修改成功！");
    }
    //分页查询
    @LogAnnotation("公告：获取分页数据")
    @RequestMapping("get")
    @ResponseBody
    public JsonResult get(String criteria,Integer pageNo,Integer pageSize){
        //获取数据
        List<Notice> list = noticeService.getNoticePageList(criteria,pageNo,pageSize);
        //获取总记录数
        int rowCount=noticeService.getRowCount();
        //创建JsonResult对象
        JsonResult result=new JsonResult();
        //设置总数与数据
        result.setCount(rowCount);
        result.setData(list);
        //返回数据
        return result;
    }

}
